//页面加载时获取待审核的企业信息
function getvarietyList(url) {
	$("#firstVarietyInfo").dataTable().fnDestroy();
	$("#firstVarietyInfo").dataTable({
		"processing" : true,
		"serverSide" : false,
		"paginationType" : "full_numbers",
		"ajax" : url,
		"aaSorting" : [ [ 0, "asc" ] ], // 默认第几个排序
		"aoColumnDefs" : [ {
			"orderable" : false,
			"aTargets" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
		} ],
		/*
		 * "dataSrc" : function(result){ return result.rows; },
		 */
		"columns" : [ {
			"data" : "id",
			"defaultContent" : ""
		/* "visible":false */
		}, {
			"data" : "varietyName", // 商品名称
			"defaultContent" : ""
		}, {
			"data" : "specification", // 规格
			"defaultContent" : ""
		}, {
			"data" : "unit", // 单位
			"defaultContent" : ""
		}, {
			"data" : "cartonSize", // 装箱规格
			"defaultContent" : ""
		}, {
			"data" : "inputCode", // 输入码
			"defaultContent" : ""
		}, {
			"data" : "manufacturer", // 生产企业
			"defaultContent" : ""
		}, {
			"data" : "registraionNo", // 产品注册证号/备案凭证编号
			"defaultContent" : ""
		}, {
			"data" : "emphasisConserve", // 是否重点养护
			"defaultContent" : ""
		}, {
			"data" : "firstType", // 首营类型
			"defaultContent" : ""
		}, {
			"data" : "ingredient", // 性能成分用途
			"defaultContent" : ""
		}, {
			"data" : "tradePrice", // 批发价
			"defaultContent" : ""
		}, {
			"data" : "retracementReason", // 回撤原因
			"defaultContent" : ""

		}, ]
	});
}

// 添加品种信息
$("#save")
		.click(
				function() {
					if (validation() == false) {
						return;
					}

					if ($("#oneTime").get(0).checked) {
						$("#oneTime").val(1);
					}

					if ($("#oneTime").get(0).checked == false) {
						$("#oneTime").val(0);
					}

					if ($("#enableSerialNum").get(0).checked) {
						$("#enableSerialNum").val(1);
					}

					if ($("#enableSerialNum").get(0).checked == false) {
						$("#enableSerialNum").val(0);
					}

					if ($("#emphasisConserve").get(0).checked) {
						$("#emphasisConserve").val(1);
					}

					if ($("#emphasisConserve").get(0).checked == false) {
						$("#emphasisConserve").val(0);
					}

					if ($("#whetherCold").get(0).checked) {
						$("#whetherCold").val(1);
					}

					if ($("#whetherCold").get(0).checked == false) {
						$("#whetherCold").val(0);
					}
					if ($("#isAddOrUpdate").val() == "add" || $("#isAddOrUpdate").val() == "copyadd") {
						// 序列化表单为json对象的方法
						$.fn.serializeJson = function() {
							var serializeObj = {};
							var array = this.serializeArray();
							$(array)
									.each(
											function() {
												if (serializeObj[this.name]) {
													if ($
															.isArray(serializeObj[this.name])) {
														serializeObj[this.name]
																.push(this.value);
													} else {
														serializeObj[this.name] = [
																serializeObj[this.name],
																this.value ];
													}
												} else {
													serializeObj[this.name] = this.value;
												}
											});
							return serializeObj;
						};
						// 得到{键:值}这种类型的数据
						var object = $("#firstVarietyForm").serializeJson();
						var ca = $("#isAddOrUpdate").val();
						$
								.ajax({
									type : "POST",
									url : "/Medical_Instrument/firstvariety/addfirstvariety?data="+ca,
									data : JSON.stringify(object),
									contentType : "application/json",
									dataType : "json",
									success : function(data) {
										if (data.result == "success") {
											// 更新成功
											layer.msg('新增成功!', {
												icon : 6,
												time : 2000
											});
											$('.modal').map(function() {
												if (!$(this).is(":hidden")) {
													$(this).modal('hide');
												}
											});
											switchflush();
										}
										if (data.result == "failed") {
											// 更新失败
											layer.msg('新增失败!', {
												icon : 5,
												time : 1000
											});
										}

									},
									error : function(data) {
										// 请求失败
										layer.msg('请求失败!', {
											icon : 5,
											time : 1000
										});
									}
								});
					} else {
						var data = $("#firstVarietyForm").serialize();
						$
								.ajax({
									type : "post",
									url : "/Medical_Instrument/firstvariety/modfirstvarietybyid",
									data : data,
									dataType : "json",
									success : function(data) {
										if (data.result != "failed") {
											layer.msg('修改成功!', {
												icon : 6,
												time : 5000
											});
											$('.modal').map(function() {
												if (!$(this).is(":hidden")) {
													$(this).modal('hide');
												}
											});
											switchflush();
										} else {
											layer.msg('请求失败!', {
												icon : 5,
												time : 1000
											});
										}
										;
									},
									error : function(data) {
										layer.msg('请求失败!', {
											icon : 5,
											time : 1000
										});
									}
								});
					}
				});

// 修改
function updatevarieties() {
	var firstNo = getSelectedNo("update");
	if (firstNo == "" || firstNo == null) {
		$("#updateVarieties").removeAttr("data-target");
		return;
	}
	$("#updateVarieties").attr("data-target", "#addBusinessVarieties");
	$.ajax({
		type : "post",
		url : "/Medical_Instrument/firstvariety/getVarietybyid",
		data : {
			"firstNo" : firstNo
		},
		dataType : "json",
		success : function(data) {
			if (data.result != "failed") {
				$("#firstVarietyForm").populateForm(formatDate(data.result));
				$('.modal').map(function() {
					if (!$(this).is(":hidden")) {
						$(this).modal('hide');
					}
				});
			} else {
				layer.msg('请求失败!', {
					icon : 5,
					time : 1000
				});
			}
			;
		},
		error : function(data) {
			layer.msg('请求失败!', {
				icon : 5,
				time : 1000
			});
		}
	});
}

// 复制新增
function copyaddvarieties() {
	var firstNo = getSelectedNo("copyadd");
	if (firstNo == "" || firstNo == null) {
		$("#copyadd").removeAttr("data-target");
		return;
	}
	$("#copyadd").attr("data-target", "#addBusinessVarieties");
	$.ajax({
		type : "post",
		url : "/Medical_Instrument/firstvariety/getVarietybyid",
		data : {
			"firstNo" : firstNo
		},
		dataType : "json",
		success : function(data) {
			if (data.result != "failed") {
				$("#firstVarietyForm").populateForm(formatDate(data.result));
				$('.modal').map(function() {
					if (!$(this).is(":hidden")) {
						$(this).modal('hide');
					}
				});
			} else {
				layer.msg('请求失败!', {
					icon : 5,
					time : 1000
				});
			}
			;
		},
		error : function(data) {
			layer.msg('请求失败!', {
				icon : 5,
				time : 1000
			});
		}
	});
}

// 删除
function deletevarieties() {
	var obj = getSelectedNo("delete");
	if (obj == null) {
		return;
	}
	layer.confirm('确认要删除吗？', function(index) {
		var firstNo = $(obj).children("td").eq(0).text();
		$.ajax({
			type : "post",
			url : "/Medical_Instrument/firstvariety/delfirstvarietybyid",
			data : {
				"firstNo" : firstNo
			},
			dataType : "json",
			success : function(data) {
				if (data.result == "success") {
					$(obj).remove();
					layer.msg('已删除!', {
						icon : 1,
						time : 5000
					});
					$('.modal').map(function() {
						if (!$(this).is(":hidden")) {
							$(this).modal('hide');
						}
					});
				} else {
					layer.msg('请求失败!', {
						icon : 5,
						time : 1000
					});
				}

			},
			error : function(data) {
				layer.msg('请求失败!', {
					icon : 5,
					time : 1000
				});
			}
		});

	});

}

// 查看
function lookvarieties() {
	attrAuditButton();
	var firstNo = getSelectedNo("look");
	if (firstNo == "" || firstNo == null) {
		$("#lookvarieties").removeAttr("data-target");
		return;
	}
	$("#lookvarieties").attr("data-target", "#addBusinessVarieties");
	$.ajax({
		type : "post",
		url : "/Medical_Instrument/firstvariety/getVarietybyid",
		data : {
			"firstNo" : firstNo
		},
		dataType : "json",
		success : function(data) {
			if (data.result != "failed") {
				$("#firstVarietyForm").populateForm(formatDate(data.result));
			} else {
				layer.msg('请求失败!', {
					icon : 5,
					time : 1000
				});
			}
			;
		},
		error : function(data) {
			layer.msg('请求失败!', {
				icon : 5,
				time : 1000
			});
		}
	});
}

function attrAuditButton() {
	buttonpublic("look");
	attr(true);
	$("#choose").removeAttr("data-target");
}
$.fn.populateForm = function(data) {
	return this.each(function() {
		var formElem, name;
		if (data == null) {
			this.reset();
			return;
		}
		for ( var i = 0; i < this.length; i++) {
			formElem = this.elements[i];
			// checkbox的name可能是name[]数组形式
			name = (formElem.type == "checkbox") ? formElem.name.replace(
					/(.+)\[\]$/, "$1") : formElem.name;
			if (data[name] == undefined)
				continue;
			switch (formElem.type) {
			case "checkbox":
				if (data[name] == "") {
					formElem.checked = false;
				} else if (data[name] == "1" || data[name] == 1) {
					formElem.checked = true;
				} else {
					// 数查找元素
					if (data[name].indexOf(formElem.value) > -1) {
						formElem.checked = true;
					} else {
						formElem.checked = false;
					}
				}
				break;
			case "radio":
				if (data[name] == "") {
					formElem.checked = false;
				} else if (formElem.value == data[name]) {
					formElem.checked = true;
				}
				break;
			case "button":
				break;
			default:
				formElem.value = data[name];
			}
		}
	});
};

// 处理日期格式
function formatDate(data) {
	var variety = eval(data);
	
	/*
	 * if (variety.varietyControl == 1) { $("#isVarietyControl").attr("checked",
	 * "checked"); } else { $("#isVarietyControl").attr("checked", false); }
	 * 
	 * var date = new Date(); if (variety.approvalValidity != "" &&
	 * variety.approvalValidity != null) { date = new
	 * Date(variety.approvalValidity); variety.approvalValidity =
	 * date.format("yyyy-MM-dd"); }
	 * 
	 * if (variety.businessPeopleDate != "" && variety.businessPeopleDate !=
	 * null) { date = new Date(variety.businessPeopleDate);
	 * variety.businessPeopleDate = date.format("yyyy-MM-dd"); }
	 * 
	 * if (variety.businessDeptDate != "" && variety.businessDeptDate != null) {
	 * date = new Date(variety.businessDeptDate); variety.businessDeptDate =
	 * date.format("yyyy-MM-dd"); }
	 * 
	 * if (variety.qualityDeptDate != "" && variety.qualityDeptDate != null) {
	 * date = new Date(variety.qualityDeptDate); variety.qualityDeptDate =
	 * date.format("yyyy-MM-dd"); }
	 * 
	 * if (variety.qualityPeopleDate != "" && variety.qualityPeopleDate != null) {
	 * date = new Date(variety.qualityPeopleDate); variety.qualityPeopleDate =
	 * date.format("yyyy-MM-dd"); }
	 * 
	 * if (variety.retracementDate != "" && variety.retracementDate != null) {
	 * date = new Date(variety.retracementDate); variety.retracementDate =
	 * date.format("yyyy-MM-dd"); }
	 */
	return variety;
}

// 判断当前有没选中行
function getSelectedNo(info) {
	var object = null;
	$("#firstVarietyInfo>tbody").find("tr").each(function() {
		if ($(this).hasClass('selected')) {
			if (info != "delete") {
				object = $(this).children("td").eq(0).text();
			} else {
				object = $(this);
			}
		}
	});
	return object;
}
